// pages/collection/collection.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sort: 'desc', //asc:升序 desc:降序
    page: 1,
    size: 10,
    totalPages: 0,
    shopName: "", 
    shopList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.queryCollShop();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.loginFlag) {
      this.queryCollShop();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //console.log("下拉加刷新")
    let _this = this;
    _this.setData({
      page: 1,
      shopList: [],
    })
    wx.showLoading({
      title: '刷新中',
    })
    _this.queryCollShop(function () {
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //console.log("上拉加载")
    let _this = this;
    if (_this.data.page < _this.data.totalPages) {
      _this.setData({
        page: _this.data.page + 1
      })
      wx.showLoading({
        title: '加载中',
      })
      _this.queryCollShop(function () {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },

  //查询收藏店铺信息
  queryCollShop(callback){
    let _this = this;
    let data = {
      "sort": _this.data.sort,
      "shopName": _this.data.shopName,
      "city": wx.getStorageSync('userInfo').city,
    }
    console.log(data)
    Api.queryCollShop(data,_this.data.page,_this.data.size).then( res => {
      console.log(res);
      _this.setData({
        shopList: _this.data.shopList.concat(res.data.dataList),
        totalPages: res.data.totalPages,
      });
      (callback && typeof (callback) === "function") && callback();
    })
  },
  
  //取消收藏
  delColl(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    Api.delColl(_this.data.shopList[index].retailShop.id).then( res => {
      console.log(res)
      let arr = _this.data.shopList;
      arr.splice(index,1)
      _this.setData({
        shopList: arr
      })
    })
  },

  //获取搜索信息
  changeName(e){
    //console.log(e)
    let _this = this;
    _this.setData({
      shopName: e.detail.value,
    })
  },

  //搜索信息
  searchShop(e){
    let _this = this;
    _this.setData({
      shopList: [],
      page: 1,
    })
    _this.queryCollShop();
  },

  //paixu
  sortChange(){
    let _this = this;
    if (_this.data.sort == "desc"){
      _this.setData({
        sort: "asc",
        page: 1,
        shopList: [],
      })
    }else{
      _this.setData({
        sort: "desc",
        page: 1,
        shopList: [],
      })
    }
    _this.queryCollShop();
  }

})